package com.qy566.gps.pulsar.admin;

import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.api.PulsarClientException;

import java.util.List;

/**
 * @author wangnaiqi
 * @description TODO
 * @date 2024/1/16
 */

public class NamespaceAdmin {

    public static void main(String[] args) throws PulsarClientException, PulsarAdminException {
        // 创建pulsar的admin管理对象
        String serviceURL = "http://10.0.0.27:32759";
        NamespaceAdmin namespaceAdmin = new NamespaceAdmin();
        PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(serviceURL).build();

//        // 创建名称空间
//        namespaceAdmin.createNamespace(admin,"gpsTenant/deviceNamespace");
//        // 获取所有的名称空间
        namespaceAdmin.namespaceList(admin,"gpsTenant");

//        namespaceAdmin.deleteNamespace(admin,"gpsTenant/expireSendMsgNamespace");


        // 关闭资源
        namespaceAdmin.close(admin);
    }

    private PulsarAdmin bulidClient(String serviceURL) throws PulsarClientException {
        return PulsarAdmin.builder().serviceHttpUrl(serviceURL).build();
    }

    private void createNamespace(PulsarAdmin admin,String namespace) throws PulsarAdminException {
        admin.namespaces().createNamespace(namespace);
    }

    private void namespaceList(PulsarAdmin admin,String tenantName) throws PulsarAdminException {
        System.out.println("获取到当前有哪些名称空间:");
        List<String> namespaces = admin.namespaces().getNamespaces(tenantName);
        for (String namespace : namespaces) {
            System.out.println(namespace);
        }
    }

    private void deleteNamespace(PulsarAdmin admin,String namespace) throws PulsarAdminException {
        admin.namespaces().deleteNamespace(namespace);
    }

    private void close(PulsarAdmin admin){
        admin.close();
    }
}
